package org.classg.edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.classg.edu.entity.Course;
import org.classg.edu.service.CourseService;
import org.classg.edu.vo.CourseInfoVo;
import org.classg.edu.vo.CoursePublishInfoVo;
import org.classg.edu.vo.CourseQuery;
import org.classg.servicebase.exception.MyException;
import org.classg.utils.R;
import org.classg.vo.CourseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-05-18
 */
@Api(description = "课程管理")
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class CourseController {

    @Autowired
    private CourseService courseService;

    @ApiOperation("添加课程基本信息")
    @PostMapping("/queryCourseInfo")
    public R queryCourseInfo(@ApiParam(name = "courseInfoVo", value = "课程基本信息") @RequestBody CourseInfoVo courseInfoVo) {
        String id = courseService.queryCourseInfo(courseInfoVo);
        // 判断id是否空值
        if (!StringUtils.isEmpty(id)) {
            return R.ok().data("courseId", id);
        } else {
            return R.error().message("添加课程基本信息失败");
        }
    }

    @ApiOperation(value = "根据课程id查询课程基本信息")
    @GetMapping("/queryCourseInfoByCourseId/{courseId}")
    public R queryCourseInfoByCourseId(@ApiParam(name = "courseId", value = "课程Id", required = true) @PathVariable String courseId) {
        CourseInfoVo courseInfoVo = courseService.queryCourseInfoByCourseId(courseId);
        return R.ok().data("courseInfoVo", courseInfoVo);
    }

    @ApiOperation(value = "更新课程基本信息")
    @PutMapping("/updateCourseInfoByCourseId")
    public R updateCourseInfoByCourseId(
            @ApiParam(name = "courseInfoVo", value = "课程基本信息", required = true) @RequestBody CourseInfoVo courseInfoVo) {
        System.out.println(courseInfoVo);
        courseService.updateCourseInfoByCourseId(courseInfoVo);
        return R.ok();
    }

    @ApiOperation("根据课程id获取课程最终发布数据")
    @GetMapping("/queryCoursePublishInfoById/{id}")
    public R queryCoursePublishInfoById(@ApiParam(name = "id", value = "课程id", required = true) @PathVariable String id) {
        CoursePublishInfoVo coursePublishInfoVo = courseService.queryCoursePublishInfoById(id);
        return R.ok().data("coursePublishInfo", coursePublishInfoVo);
    }

    @ApiOperation("根据课程id修改课程状态")
    @PutMapping("/updateCourseStatusById/{id}")
    public R updateCourseStatusById(@ApiParam(name = "id", value = "课程id", required = true) @PathVariable String id) {
        Course course = new Course();
        course.setId(id);
        course.setStatus("Normal");// 修改课程状态
        boolean temp = courseService.updateById(course);
        if (!temp) {
            throw new MyException(20001, "修改课程状态失败！");
        } else {
            return R.ok();
        }
    }

    @ApiOperation("课程列表带分页")
    @PostMapping("/queryCoursePage/{current}/{limit}")
    public R queryCoursePage(
            @ApiParam(name = "current", value = "当前页码", required = true)
            @PathVariable Long current,
            @ApiParam(name = "limit", value = "每页显示的数量", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "courseQuery", value = "查询对象", required = false)
            @RequestBody(required = false) CourseQuery courseQuery) {
        // 多条件组合查询，使用动态sql来拼接
        String title = courseQuery.getTitle();
        String teacherName = courseQuery.getTeacherName();
        String status = courseQuery.getStatus();
        String begin = courseQuery.getBegin();
        String end = courseQuery.getEnd();
        IPage<Course> page = new Page<>(current, limit);
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        // 判断添加是否为空，如果不为空拼接条件
        if (!StringUtils.isEmpty(title)) {
            queryWrapper.like("title", title);
        }
        if (!StringUtils.isEmpty(teacherName)) {
            queryWrapper.eq("teacher_id", teacherName);
        }
        if (!StringUtils.isEmpty(status)) {
            queryWrapper.eq("status", status);
        }
        if (!StringUtils.isEmpty(begin)) {
            queryWrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            queryWrapper.le("gmt_create", end);
        }

        // 根据时间排序
        queryWrapper.orderByDesc("gmt_create");

        IPage<Course> iPage = courseService.page(page, queryWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("Total", iPage.getTotal());// 总记录数
        map.put("rows", iPage.getRecords());// 数据集合
        return R.ok().data(map);
    }


    @ApiOperation("根据id删除课程")
    @DeleteMapping("/deleteCourseById/{id}")
    public R deleteCourseById(@ApiParam(name = "id", value = "课程id", required = true) @PathVariable String id) {
        Boolean result = courseService.deleteCourseById(id);
        if (!result) {
            throw new MyException(20001, "根据id删除课程失败！");
        } else {
            return R.ok();
        }
    }

    @ApiOperation("根据课程id查询课程数据")
    @GetMapping("/queryCourseById/{id}")
    public CourseVo queryCourseById(@ApiParam(name = "id", value = "课程id", required = true) @PathVariable String id) {
        return courseService.queryCourseById(id);
    }

}

